home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT68 / WGT68B.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  5.3 KB  |  260 lines

  1. #include <wgt5.h>
  2.  
  3. /*
  4. ==============================================================================
  5.               WordUp Graphics Toolkit Version 5.0
  6.                  Demonstration Program 68b
  7.  
  8.  Same as wgt68a.c, but with shadows.
  9.  
  10.  *** PROJECT ***
  11.  This program requires the WGT5_WC.LIB file to be linked.
  12.  
  13.  *** DATA FILES ***
  14.  STREET.PAL, RUN.SPR
  15.                                WATCOM C++ VERSION
  16. ==============================================================================
  17. */
  18.  
  19.  
  20. unsigned char shadowtable[256]; /* Shadow table */
  21.  
  22. color pal[256];         /* The palette used for every graphic image */
  23. block sprites[200];     /* Array of images for the running robot */
  24.  
  25. block background;       /* Holds the background scrolling image */
  26. block work;             /* Page for constructing each frame */
  27. int oldmode;
  28.  
  29. /* A structure which holds the scrolling values for each horizontal line */
  30. typedef struct
  31.  {
  32.   int x;                /* Current x value, shifted by 8 */
  33.   int increment;        /* fixed point increment */
  34.  } line_scroll;
  35. line_scroll lines[80];  /* 80 scrolling lines along the ground */
  36.  
  37. /* A simple sprite structure */
  38. typedef struct
  39.  {
  40.   int x;
  41.   int y;
  42.   int anm;              /* Sprite number */
  43.  } sprite;
  44. sprite people[5];
  45.  
  46.  
  47. int backx = 0;          /* X value for the scrolling rocks */
  48. int backinc;            /* X increment for scrolling rocks */
  49.  
  50.  
  51.  
  52.  
  53. /* Loads the graphics files, and allocates buffers */
  54. void load_graphics (void)
  55. {
  56.  work = wallocblock (320, 200);
  57.  /* Allocate a work buffer */
  58.  
  59.  wloadsprites (pal, "run.spr", sprites, 0, 199);
  60.  background = wloadpak ("street.pak");
  61.  wsetpalette (0, 255, pal);
  62. }
  63.  
  64.  
  65.  
  66. /* Frees the buffers and sprites */
  67. void free_graphics (void)
  68. {
  69.  wfreesprites (sprites, 0, 199);
  70.  wfreeblock (background);
  71.  wfreeblock (work);
  72. }
  73.  
  74.  
  75. /* Set up the initial scrolling values */
  76. void init_lines (void)
  77. {
  78. int i;
  79. int inc;
  80.  
  81.  inc = 128;     /* slowest scrolling speed (128/256 of a pixel */
  82.  
  83.  backx = 0;     /* rocks x value */
  84.  backinc = inc; /* rocks same speed as the ground */
  85.  
  86.  for (i = 0; i < 80; i++)
  87.   {
  88.    lines[i].x = 0;              /* clear out the x value */
  89.    lines[i].increment = inc;    /* set the scroll speed */
  90.    inc += 32;                   /* Make the next row move faster */
  91.   }
  92.  
  93. }
  94.  
  95.  
  96.  
  97. /* Construct the background image */
  98. void animate_lines (void)
  99. {
  100. block source1, source2;
  101. block dest1, dest2;
  102. block origsource, origdest;
  103. int i;
  104. int x;
  105.  
  106.  wcopyscreen (0, 0, 319, 51, background, 0, 0, work);
  107.  /* Draw the moon stationary */
  108.  
  109.  
  110.  /* Scroll the rocks */
  111.  backx += backinc;
  112.  if (backx >= 81920)   /* 81920 is 320 << 8 */
  113.      backx -= 81920;
  114.  
  115.  x = backx >> 8;
  116.  
  117.  /* Copy the rocks */
  118.  wcopyscreen (x, 52, 319, 119, background, 0, 52, work);
  119.  if (x > 0)
  120.    wcopyscreen (0, 52, x-1, 119, background, 320 - x, 52, work);
  121.  
  122.  
  123.  
  124.  origdest = abuf + 120 * 320;           /* First row to copy */
  125.  origsource = background + 120 * 320;   /* First row to copy */
  126.  
  127.  for (i = 0; i < 80; i++)
  128.   {
  129.    /* Scroll this line */
  130.    lines[i].x += lines[i].increment;
  131.    if (lines[i].x >= 81920)   /* 81920 is 320 << 8, wraps scroll around */
  132.       lines[i].x -= 81920;
  133.  
  134.    
  135.    x = lines[i].x >> 8;
  136.    /* Get the x coord */
  137.  
  138.    dest1 = origdest + i * 320;
  139.    dest2 = dest1 + (319 - x);
  140.    source1 = origsource + i * 320;
  141.    source2 = source1 + x;
  142.    
  143.    /* Copy the line in two steps */
  144.    memcpy (dest1, source2, 320 - x);
  145.    if (x > 0)
  146.     memcpy (dest2, source1, x + 1);
  147.    
  148.   }
  149.  
  150. }
  151.  
  152.  
  153.  
  154. /* Animates and displays the running man */
  155. void animate_man (void)
  156. {
  157.  people[0].anm++;
  158.  if (people[0].anm > 29)
  159.    people[0].anm = 0;
  160.  
  161.  wputblock_shade (people[0].x, 158, sprites[people[0].anm+30],
  162.           shadowtable, SHADE_SHADOW);
  163.  wputblock (people[0].x, people[0].y, sprites[people[0].anm], 1);
  164.  
  165. }
  166.  
  167.  
  168.  
  169.  
  170. void wcreate_shadow_table (color *palette)
  171. {
  172. float fr, fg, fb;
  173. long ir, ig, ib;
  174.  
  175. long absr, absg, absb;
  176.  
  177.  
  178. int r,g,b;
  179.  
  180. short col;
  181. short findcol;
  182.  
  183. unsigned long lowest;
  184. unsigned char bestfit;
  185. unsigned long coldif;
  186.  
  187.  for (col = 0; col < 256; col++)
  188.   {
  189.  
  190.    fr = (float)palette[col].r * (0.5);
  191.    fg = (float)palette[col].g * (0.5);
  192.    fb = (float)palette[col].b * (0.5);
  193.  
  194.    ir = fr;
  195.    ig = fg;
  196.    ib = fb;
  197.  
  198.    lowest = 655350;
  199.    for  (findcol = 0; findcol < 256; findcol++)
  200.     {
  201.       absr = abs ( (long)palette[findcol].r - ir);
  202.       absg = abs ( (long)palette[findcol].g - ig);
  203.       absb = abs ( (long)palette[findcol].b - ib);
  204.  
  205.       coldif = absr + absg + absb;
  206.       if  ((coldif < lowest) && (findcol != col))
  207.       {
  208.     lowest = coldif;
  209.     bestfit = findcol;
  210.       }
  211.     }
  212.    shadowtable[col] = bestfit;
  213.   }
  214.  
  215. }
  216.  
  217.  
  218.  
  219.  
  220. void main (void)
  221. {
  222.   oldmode = wgetmode ();
  223.   if (!vgadetected ())
  224.   {
  225.     printf ("VGA is required to run this program...");
  226.     exit (1);
  227.   }
  228.  
  229.   printf ("WGT Example #68b\n\n");
  230.   printf ("This example adds shadows to the running robot.\n");
  231.   printf ("\nPress any key to begin.\n");
  232.   getch ();
  233.  
  234.   vga256 ();
  235.  
  236.  load_graphics ();
  237.  
  238.  init_lines ();
  239.  
  240.  /* Set the position of the running man */
  241.  people[0].x = 120;
  242.  people[0].y = 50;
  243.  people[0].anm = 0;
  244.  
  245.  wcreate_shadow_table (pal);
  246.  
  247.  do { 
  248.   wsetscreen (work);
  249.   animate_lines ();
  250.   animate_man ();   
  251.   wnormscreen ();
  252.   wretrace ();
  253.   wputblock (0, 0, work, 0);
  254.   } while (!kbhit ());
  255.  
  256.  free_graphics ();
  257.  wsetmode (oldmode);
  258. }
  259.  
  260.